package com.jimo.transadb

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.*
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import frpclib.Frpclib
import kotlinx.coroutines.*
import java.io.File

class ForegroundService : Service() {
    //    private lateinit var helper: VibrationHelper
    private val CHANNEL_ID: String = "frpcs_adb_channel"
    private val NOTIFICATION_ID = 1001
    private var isRunning = true // 标记操作是否正在运行

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate() {
        super.onCreate()
//        helper = VibrationHelper(applicationContext)
        createNotificationChannel()
    }

    override fun onBind(intent: Intent): IBinder {
        return Binder()
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel() {
        val notificationManager = getSystemService(
            NotificationManager::class.java
        )
        notificationManager.createNotificationChannel(
            NotificationChannel(
                CHANNEL_ID,
                "My Channel",
                NotificationManager.IMPORTANCE_DEFAULT
            )
        )
    }

    override fun onDestroy() {
        super.onDestroy()
        isRunning = false
//        helper.stopVibration()
        try {
            Frpclib.stop()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        stopForeground(true)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
//        helper.startVibration()
        Thread {

            // 在循环中持续执行操作，直到 isRunning 变为 false
            while (isRunning) {
                Log.i("emmmmmmm onStartCommand", "onStartCommand:运行frpc中 ")
                val file = File(filesDir, "frpc.ini")
                if (file.exists()) {
                    Frpclib.run(file.absolutePath)
                }
                Log.i("emmmmmmm onStartCommand", "onStartCommand:睡眠10秒 ")
                // 睡眠一段时间，等待下一次操作
                Thread.sleep(10000) // 例如，10秒钟
            }
//            helper.stopVibration()
            Log.i("emmmmmmm onStartCommand", "onStartCommand: 停止frpc中")
            // 当操作结束后，停止前台服务
            stopForeground(true)
            stopSelf()
        }.start()

        // 在这里显示通知，表示服务已经启动
        startForeground(NOTIFICATION_ID, getNotification())

        return START_STICKY
    }

    private fun getNotification(): Notification {
        val notificationIntent = Intent(this, MainActivity::class.java)
        val pendingIntent = PendingIntent.getActivity(
            this, 0, notificationIntent,
            PendingIntent.FLAG_MUTABLE
        )
        val builder: NotificationCompat.Builder = NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("adb frpcs正在前台运行")
            .setContentText("该服务可以保障被控端不离线")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
            .setContentIntent(pendingIntent)
        return builder.build()
    }
}